package base;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

import coom.Constants;
import event.LoginEvent;
import ui.activity.login.LoginAct;
import util.ToastUtils;

import static com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_FAIL;
import static com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_LOADING;
import static com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_SUCCESS;

/**
 * by BF00233 on 2018/12/15.
 */
public abstract class BaseActivity extends AppCompatActivity {

    private MyHandler mHandler;

    private QMUITipDialog mLoadingDialog;

    private QMUITipDialog mSuccessTipDialog;
    private QMUITipDialog mFailureTipDialog;

    public abstract int getLayoutId();

    private static class MyHandler extends BFHandler<BaseActivity> {

        public MyHandler(BaseActivity target) {
            super(target);
        }

        @Override
        public void handle(BaseActivity target, Message msg) {
            switch (msg.what) {
                case 1:
                    target.hideSuccessTipDialog();
                    break;
                case 2:
                    target.hideFailureTipDialog();
                    break;
            }

        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        EventBus.getDefault().register(this);
        mHandler = new MyHandler(this);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    /**
     * 302跳转登录
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginEvent(LoginEvent event) {
        ToastUtils.shortToast("登录状态失效，请重新登录");
        finish();
        startAct(LoginAct.class, null);
    }


    public void startAct(Class clz, Serializable serializable) {
        Intent intent = new Intent(this, clz);
        if (serializable != null) {
            intent.putExtra(Constants.KEY, serializable);
        }
        startActivity(intent);
    }

    protected void showProgressDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new QMUITipDialog.Builder(this)
                    .setIconType(ICON_TYPE_LOADING)
                    .setTipWord("加载中")
                    .create();
            mLoadingDialog.setCancelable(false);
        }
        mLoadingDialog.show();
    }

    protected void dismissProgressDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    protected void showSuccessTipDialog(String message) {
        mSuccessTipDialog = new QMUITipDialog.Builder(this)
                .setIconType(ICON_TYPE_SUCCESS)
                .setTipWord(message)
                .create();
        mSuccessTipDialog.show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Message message1 = mHandler.obtainMessage(1);
                mHandler.sendMessage(message1);
            }
        }, 1500);
    }

    protected void hideSuccessTipDialog() {
        if (mSuccessTipDialog != null) {
            mSuccessTipDialog.dismiss();
        }
    }

    protected void showFailureTipDialog(String message) {
        mFailureTipDialog = new QMUITipDialog.Builder(this)
                .setIconType(ICON_TYPE_FAIL)
                .setTipWord(message)
                .create();
        mFailureTipDialog.show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Message message1 = mHandler.obtainMessage(2);
                mHandler.sendMessage(message1);
            }
        }, 1500);
    }

    protected void hideFailureTipDialog() {
        if (mFailureTipDialog != null) {
            mFailureTipDialog.dismiss();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

}
